// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.commons.ui.jface.fieldassist;

import org.eclipse.jface.fieldassist.ControlDecoration;
import org.eclipse.jface.fieldassist.FieldDecoration;
import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
import org.eclipse.jface.fieldassist.IControlContentAdapter;
import org.eclipse.swt.widgets.Control;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-2-29 qiang.zhang $
 * 
 */
public abstract class SmartField {

    ControlDecoration controlDecoration;

    Control control;

    IControlContentAdapter contentAdapter;

    FieldDecoration errorDecoration, warningDecoration;

    public SmartField(ControlDecoration dec, Control control, IControlContentAdapter adapter) {
        this.controlDecoration = dec;
        this.contentAdapter = adapter;
        this.control = control;
    }

    public boolean isRequiredField() {
        return true;
    }

    public boolean hasQuickFix() {
        return false;
    }

    public void quickFix() {
        // do nothing
    }

    public boolean hasContentAssist() {
        return false;
    }

    public void dispose() {
        // do nothing
    }

    public FieldDecoration getErrorDecoration() {
        if (errorDecoration == null) {
            FieldDecoration standardError;
            if (hasQuickFix()) {
                standardError = FieldDecorationRegistry.getDefault().getFieldDecoration(
                        FieldDecorationRegistry.DEC_ERROR_QUICKFIX);
            } else {
                standardError = FieldDecorationRegistry.getDefault().getFieldDecoration(FieldDecorationRegistry.DEC_ERROR);
            }
            if (getErrorMessage() == null) {
                errorDecoration = standardError;
            } else {
                errorDecoration = new FieldDecoration(standardError.getImage(), getErrorMessage());
            }
        }
        return errorDecoration;

    }

    public FieldDecoration getWarningDecoration() {
        if (warningDecoration == null) {
            FieldDecoration standardWarning = FieldDecorationRegistry.getDefault().getFieldDecoration(
                    FieldDecorationRegistry.DEC_WARNING);
            if (getWarningMessage() == null) {
                warningDecoration = standardWarning;
            } else {
                warningDecoration = new FieldDecoration(standardWarning.getImage(), getWarningMessage());
            }
        }
        return warningDecoration;
    }

    public String getContents() {
        return contentAdapter.getControlContents(control);
    }

    public void setContents(String contents) {
        contentAdapter.setControlContents(control, contents, contents.length());
    }

    public abstract boolean isValid();

    public abstract boolean isWarning();

    public String getErrorMessage() {
        return null;
    }

    public String getWarningMessage() {
        return null;
    }

    /**
     * Getter for control.
     * 
     * @return the control
     */
    public Control getControl() {
        return this.control;
    }

    /**
     * Getter for controlDecoration.
     * 
     * @return the controlDecoration
     */
    public ControlDecoration getControlDecoration() {
        return this.controlDecoration;
    }

}
